package com.dokimall.order.interceptor;

import com.dokimall.common.constant.AuthServerConstant;
import com.dokimall.common.vo.MemberVo;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @ClassName: OrderInterceptor
 * @Description:
 * @Author
 * @Date 2021/10/26
 * @Version 1.0
 */
@Component
public class OrderInterceptor implements HandlerInterceptor {

    public static ThreadLocal<MemberVo> threadLocal = new ThreadLocal<>();

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String uri = request.getRequestURI();
        AntPathMatcher pathMatcher = new AntPathMatcher();
        boolean match = pathMatcher.match("/order/order/getInfo/**", uri);
        boolean match2 = pathMatcher.match("/payed/notify", uri);

        if (match || match2){
            return true;
        }

        MemberVo memberVo = (MemberVo) request.getSession().getAttribute(AuthServerConstant.LOGIN_USER);
        if(memberVo!=null){
            threadLocal.set(memberVo);
            return true;
        }else {
            // 拦截未登录用户进行登录
            request.getSession().setAttribute("msg","购买商品需要进行登录");
            response.sendRedirect("http://auth.dokimall.com/login.html");
            return false;
        }
    }
}
